package com.mrper.analysistant

import com.google.auto.service.AutoService
import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.TypeName
import com.squareup.javapoet.TypeSpec
import java.io.IOException
import javax.annotation.processing.AbstractProcessor
import javax.annotation.processing.Filer
import javax.annotation.processing.Messager
import javax.annotation.processing.ProcessingEnvironment
import javax.annotation.processing.Processor
import javax.annotation.processing.RoundEnvironment
import javax.lang.model.SourceVersion
import javax.lang.model.element.Modifier
import javax.lang.model.element.TypeElement
import javax.lang.model.util.Elements

@AutoService(Processor::class)
class MyProcessor : AbstractProcessor() {

    private var mFiler: Filer? = null

    private var mMessager: Messager? = null

    private var mElementUtils: Elements? = null

    override fun init(processingEnv: ProcessingEnvironment?) {
        super.init(processingEnv)
        this.mFiler = processingEnv?.filer
        this.mMessager = processingEnv?.messager
        this.mElementUtils = processingEnv?.elementUtils
    }

    override fun process(set: MutableSet<out TypeElement>?, roundEnvironment: RoundEnvironment?): Boolean {
        try {
            val finderClass = TypeSpec.interfaceBuilder("MyGeneratedInterface")
                .addModifiers(Modifier.PUBLIC)
                .addMethod(MethodSpec.methodBuilder("sayHello").addModifiers(Modifier.PUBLIC).returns(TypeName.VOID).build())
                .build()
            val javaFile = JavaFile.builder("com.mrper.debugger", finderClass).build()
            javaFile.writeTo(mFiler)
        } catch (e: IOException) {
            e.printStackTrace()
        }
        return true
    }

    override fun getSupportedAnnotationTypes(): MutableSet<String> {
        // return super.getSupportedAnnotationTypes()
        return LinkedHashSet<String>().apply {
            add(Override::class.java.canonicalName)
        }
    }

    override fun getSupportedSourceVersion(): SourceVersion = SourceVersion.RELEASE_17

}